# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer:
pkgname=ponyc
pkgver=0.33.2
pkgrel=0
pkgdesc="An open-source, actor-model, capabilities-secure, high performance programming language"
url="https://www.ponylang.org/"
arch="all !s390x !ppc64le !mips !mips64"
license="BSD-2-Clause"
_llvmver=9
depends="binutils-gold"
checkdepends="openssl-dev pcre2-dev"
makedepends="libexecinfo-dev llvm$_llvmver-dev ncurses-dev paxmark
	zlib-dev linux-headers"
options="!check"
subpackages="$pkgname-static $pkgname-dev $pkgname-doc"
source="ponyc-$pkgver.tar.gz::https://github.com/ponylang/ponyc/archive/$pkgver.tar.gz
	more-portable-ln.patch
	makefile-remove-march-mtune.patch
	rename-SO_RCVTIMEO-SO_SNDTIMEO-with-OLD-suffix.patch
	"

export LLVM_CONFIG=/usr/lib/llvm$_llvmver/bin/llvm-config
# Required to not crash gold: https://sourceware.org/bugzilla/show_bug.cgi?id=23856
export CFLAGS="$CFLAGS -fPIE"

build() {
	make \
		config=release \
		default_pic=true \
		link=llvm-dynamic \
		verbose=true

	paxmark m build/release/ponyc \
		build/release/libponyc.tests

	mkdir docs
	build/release/ponyc packages/stdlib -rexpr -g -o docs
}

check() {
	make test
}

package() {
	make \
		config=release \
		default_pic=true \
		link=llvm-dynamic \
		ponydir="$pkgdir"/usr/lib/pony/$pkgver \
		prefix="$pkgdir"/usr \
		install

	# links to $pkgdir
	ln -sf /usr/lib/pony/$pkgver/lib/native/libponyrt.a "$pkgdir"/usr/lib/libponyrt.a
	ln -sf /usr/lib/pony/$pkgver/lib/native/libponyrt-pic.a "$pkgdir"/usr/lib/libponyrt-pic.a
	ln -sf /usr/lib/pony/$pkgver/lib/native/libponyc.a "$pkgdir"/usr/lib/libponyc.a
	ln -sf /usr/lib/pony/$pkgver/bin/ponyc "$pkgdir"/usr/bin/ponyc

	# We install these manually in dev()
	rm -r "$pkgdir"/usr/include/

	mkdir -p "$pkgdir"/usr/share/doc/$pkgname
	cp -r docs/stdlib-docs/docs "$pkgdir"/usr/share/doc/$pkgname/stdlib
}

# Note: libponyrt*.a and libponyc.a are needed in runtime.
dev() {
	pkgdesc="$pkgdesc (development files)"
	depends="$pkgname=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"/usr/
	mv "$pkgdir"/usr/lib/pony/$pkgver/include "$subpkgdir"/usr
}

sha512sums="cc02bdac029325689fbe7a5c9e8c9f8a8adf9346e17a6c626c9eca8dcae110c3cf4fb9af348b603e9804dff97183e02687f092d76b62e4bb13e571cd1c7585e8  ponyc-0.33.2.tar.gz
38aa40f6000dcb823be7b542cdad8d04b6ac4cd0a4fff1a472520a7cb71631fb34e5bee74914fe559674594dd901aede91390809e143dd96c5d68a16f8ac196c  more-portable-ln.patch
ce98aa2e33d057714703364509afd15e42ba53b5fefd42b66b9996a69036767595b868ddda592695bb109f09a7e1de118a53f262c2b23cbdf0b84063245f8b1b  makefile-remove-march-mtune.patch
1c59f43f8aae369a0fc05c23082e86562d46f943a54e7490d5cb4f5087096213b44d7bda7fde9a1a536536c6f40fa29b9a88b3ac6c0e8e1d33c4a38f9201bc90  rename-SO_RCVTIMEO-SO_SNDTIMEO-with-OLD-suffix.patch"
